fix(anthropic): Only finish relevant spans in .create() patches#5716
Conversation
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛Anthropic
Documentation 📚
Internal Changes 🔧
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 10 passed | Total: 10 | Pass Rate: 100% | Execution Time: 2.87s All tests are passing successfully. ❌ Patch coverage is 20.00%. Project has 15227 uncovered lines. Files with missing lines (1)
Generated by Codecov Action |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
ericapisani
left a comment
There was a problem hiding this comment.
Is there any test coverage that we can add here to confirm this behaviour? Otherwise LGTM
…opic/remove-get-current-span
In my view a regression test has limited use in this case because an integration should not be touching spans created outside the integration (unless it's for an agreed-upon reason, i.e., not duplicating spans between For the future, here's a script that verifies that demonstrates the edge case I'm addressing below. The import sentry_sdk
from sentry_sdk.tracing import SPANSTATUS
from sentry_sdk.integrations.anthropic import AnthropicIntegration
from anthropic import Anthropic
sentry_sdk.init(
dsn="",
integrations=[AnthropicIntegration()],
traces_sample_rate=1.0,
)
def main():
client = Anthropic(api_key="dummy")
with sentry_sdk.start_transaction(name="parent", op="test") as transaction:
transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
try:
client.messages.create(max_tokens=1024, model="model", stream=False)
except Exception as e:
print(f"Expected exception: {e}")
if __name__ == "__main__":
main() |
…opic/remove-get-current-span

Description
Anthropic spans are only created in
_sentry_patched_create()and_sentry_patched_create_async().Exit spans in these functions when the
anthropiclibrary function raises an exception.Resolves an edge case where
_sentry_patched_create()or_sentry_patched_create_async()exits early and does not create a span andget_current_span()therefore returns a non-anthropic span.Issues
Reminders
tox -e linters.feat:,fix:,ref:,meta:)